/**
 *	---------------------------------------------------------------------------
 *	COMP 6231/FALL 2013
 *	Assignment - 01
 *	@author Talha Rahman (1296884)
 *	---------------------------------------------------------------------------
 *	This class loads and contains necessary configuration parameters for the 
 *	RMI server {@link StationServer} application.
 *	@see {@link StationServer}
 *	  
 */
package ca.concordia.comp6231.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ServerConfiguration {
	
	private String serverName;
	private List<String> availableServerNames;
	private HashMap<String, String> hostNameList;
	private HashMap<String, Integer> portNoList;
	private HashMap<String, Integer> udpPortNoList;
	private HashMap<String, List<String>> statusMap;
	
	public ServerConfiguration(String serverName) throws Exception{
		loadAvailableServerNames();
		
		if(!availableServerNames.contains(serverName)){
			throw new Exception("Invalid Server Name specified.");
		}
		
		this.serverName = serverName;
		setSecurityPolicy();
		loadHostNameList();
		loadPortConfiguration();
		
		loadStatusMap();
	}
	
	private static void setSecurityPolicy(){
		System.setProperty("java.security.policy","file:./security.policy");
	}
	
	private void loadAvailableServerNames(){
		availableServerNames = new ArrayList<String>();
		availableServerNames.add("SPVM");
		availableServerNames.add("SPL");
		availableServerNames.add("SPB");
	}
	
	private void loadPortConfiguration(){
		portNoList = new HashMap<String, Integer>();
		udpPortNoList = new HashMap<String, Integer>();
		int startingPort = 2020, startingUDPPort = 6780;
		for(String serverName:availableServerNames){
			portNoList.put(serverName, startingPort++);
			udpPortNoList.put(serverName, startingUDPPort++);
		}
	}
	
	private void loadHostNameList(){
		hostNameList = new HashMap<String, String>();
		for(String serverName:availableServerNames){
			hostNameList.put(serverName, "localhost");
		}
	}
	
	private void loadStatusMap(){
		statusMap = new HashMap<String, List<String>>();
		List<String> statusList = new ArrayList<String>();
		
		statusList.add("InProgress");
		statusList.add("Solved");
		statusMap.put("CR",statusList);
		
		statusList = null;
		statusList = new ArrayList<String>();
		
		statusList.add("Missing");
		statusList.add("Found");
		
		statusMap.put("MR",statusList);
	}
	
	public boolean isValidStatus(String recordType, String status){
		return statusMap.get(recordType).contains(status);
	}
	
	public Integer getPortNo(){
		return this.portNoList.get(serverName);
	}
	
	public Integer getUDPPortNo(){
		return this.udpPortNoList.get(serverName);
	}
	
	public Integer getUDPPortNo(String serverName){
		return this.udpPortNoList.get(serverName);
	}
	
	public String getHostName(){
		return hostNameList.get(this.serverName);
	}
	
	public String getHostName(String serverName){
		return hostNameList.get(serverName);
	}
	
	public List<String> getAvailableServerNames(){
		return availableServerNames;
	}
}
